package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.szb.R;

import java.util.ArrayList;

import com.example.administrator.szb.activity.base.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.example.administrator.szb.fragments.fragment_forMy.FragmentGZGL;

/**
 * Created by D N on 2017/11/6.
 */

public class GZGLActivity extends BaseActivity {
    @Bind(R.id.gzgl_wdgz)
    RadioButton gzglWdgz;
    @Bind(R.id.gzgl_gzwd)
    RadioButton gzglGzwd;
    @Bind(R.id.gzgl_radiogroup)
    RadioGroup gzglRadiogroup;
    @Bind(R.id.gzgl_content)
    FrameLayout gzglContent;

    int type = -1;//1,代表我的关注，2代表关注我的

    private FragmentManager fragmentManager;
    private ArrayList<Fragment> fragmentArrayList;
    private FragmentGZGL fragment_wtcdwt,fragment_wsddwt;
    private FragmentTransaction fragmentTransaction;
    private Bundle bundle = new Bundle();



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gzgl_activiyt);
        ButterKnife.bind(this);
        initToolbar(R.id.gzgl_toolbar_include,"关注管理");
        initFragment();
        initView();
    }

    private void initView() {
        gzglRadiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.gzgl_gzwd:
                        changeStatu(2);
                        break;
                    case R.id.gzgl_wdgz:
                        changeStatu(1);
                        break;
                }
            }
        });
    }

    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        fragmentArrayList = new ArrayList<Fragment>();
        changeStatu(1);
    }

    private void changeStatu(int i) {
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (i){
            case 1:
                if(fragment_wtcdwt == null){
                    fragment_wtcdwt = new FragmentGZGL();
                    bundle.putInt("type",1);
                    fragment_wtcdwt.setArguments(bundle);
                    changeFragment(fragment_wtcdwt);
                }else {
                    fragmentTransaction.show(fragment_wtcdwt);
                }
                break;
            case 2:
                if(fragment_wsddwt == null){
                    fragment_wsddwt = new FragmentGZGL();
                    bundle.putInt("type",2);
                    fragment_wsddwt.setArguments(bundle);
                    changeFragment(fragment_wsddwt);
                }else {
                    fragmentTransaction.show(fragment_wsddwt);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    private void changeFragment(FragmentGZGL fragment) {
        fragmentArrayList.add(fragment);
        fragmentTransaction.add(R.id.gzgl_content, fragment);
        fragmentTransaction.show(fragment);
    }

    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragmentArrayList) {
            fragmentTransaction.hide(fragment);
        }
    }
}
